using UnityEngine;

public class ControllerRamp : ControllerGeneric
{
	public float length = 1f;

	public float width = 1f;

	public float height = 1f;

	public GameObject ramp;

	public JumpRampType rampType = JumpRampType.Flat;

	private bool staticState;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		SetNames();
		SetParenting();
		base.name = "will_destroy_" + base.name;
		if (Application.isPlaying)
		{
			Object.Destroy(base.gameObject);
		}
		else
		{
			Object.DestroyImmediate(base.gameObject);
		}
	}

	private void Update()
	{
		if (!Application.isEditor || Application.isPlaying)
		{
			return;
		}
		SetupRemoteControl();
		if (IsRemoteControlActive())
		{
			base.transform.position = remoteControl.transform.position;
			base.transform.rotation = remoteControl.transform.rotation;
			width = remoteControl.transform.localScale.x;
			height = remoteControl.transform.localScale.y;
			length = remoteControl.transform.localScale.z;
			base.transform.localScale = remoteControl.transform.localScale;
		}
		if (length < 0.0001f)
		{
			length = 0.0001f;
		}
		if (width < 0.0001f)
		{
			width = 0.0001f;
		}
		if (height < 0.0001f)
		{
			height = 0.0001f;
		}
		if (remoteControl != null)
		{
			if (remoteControl.manualScale)
			{
				remoteControl.scale = new Vector3(remoteControl.scale.x, remoteControl.scale.y, remoteControl.scale.z);
			}
			else
			{
				remoteControl.transform.localScale = new Vector3(width, height, length);
			}
		}
		Setup();
		SetupUVs();
	}

	private void Setup()
	{
		AssureParenting();
		SetNames();
	}

	private void SetStatic(bool newStaticState = true)
	{
		if (newStaticState != staticState)
		{
			staticState = newStaticState;
			ramp.isStatic = staticState;
		}
	}

	private void SetParenting()
	{
		ramp.transform.parent = base.transform.parent;
	}

	private void SetNames()
	{
		ramp.name = "Ramp";
		if (rampType == JumpRampType.Curved)
		{
			ramp.name += "Curved";
		}
		else if (rampType == JumpRampType.Flat)
		{
			ramp.name += "Flat";
		}
		else if (rampType == JumpRampType.Circular)
		{
			ramp.name += "Circular";
		}
	}

	public void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = new Vector3(width, height, length);
			remoteControl.gameObject.AddComponent<PresetsRamp>();
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		CopyParentingFromRemoteControl(rc);
	}

	public void SetupUVs()
	{
		float num = 1f / 3f;
		Mesh mesh = ramp.GetComponent<MeshFilter>().mesh;
		Vector2[] uv = mesh.uv;
		Vector3[] normals = mesh.normals;
		Vector3[] vertices = mesh.vertices;
		for (int i = 0; i < vertices.Length; i++)
		{
			if (!isAxisAligned(normals[i]))
			{
				float num2 = Mathf.Sign(vertices[i].x);
				uv[i].x = 0.5f + num * num2 * (width / 2f);
			}
		}
		mesh.uv = uv;
	}

	private bool isAxisAligned(Vector3 v)
	{
		if ((v.x == 0f && v.y == 0f) || (v.x == 0f && v.z == 0f) || (v.y == 0f && v.z == 0f))
		{
			return true;
		}
		return false;
	}
}
